//
//  MyCertificateViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/10.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "MyCertificateViewController.h"
#import "MyCertificateTableViewCell.h"
#import "CertificateDetailViewController.h"
@interface MyCertificateViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIImageView * headerView;
@property (nonatomic, strong)UILabel * accountContentLab;//账户余额具体数额
@property (nonatomic, strong)NSMutableArray * cellArr;
@end
@implementation MyCertificateViewController(networking)
- (void)getVoucherList{
    NSDictionary *parameters = @{
                                 @"userId":[UserModel sharedInstanced].userID,
                                 @"cookie":[UserModel sharedInstanced].cookie,
                                 };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"plan/getVoucherList" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"msg"] isEqualToString:@"success"]) {
            [self.cellArr addObjectsFromArray:responseObject[@"list"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end

@implementation MyCertificateViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    [self getVoucherList];//获得互助历史记录
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    BOOL isHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isHomePage animated:YES];
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 68/WIDTH_6_SCALE;
        [_tableView registerClass:[MyCertificateTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MyCertificateTableViewCell class])];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.headerView;
    }return _tableView;
}
- (UIImageView *)headerView{
    if (!_headerView) {
        _headerView = [[UIImageView alloc] init];
        _headerView.image = [UIImage imageNamed:@"maself_bg"];
        _headerView.userInteractionEnabled = YES;
        //navigationItem
        UILabel * pageTitleLab = [[UILabel alloc] init];
        //返回按钮
        UIButton * backBtn = [[UIButton alloc] init];
        if (Is_Iphone_X) {
            _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 264/WIDTH_6_SCALE + 24);
            backBtn.frame = CGRectMake(18/WIDTH_6_SCALE, 14/WIDTH_6_SCALE + 24 + 20, 24/WIDTH_6_SCALE, 30/WIDTH_6_SCALE);
            pageTitleLab.frame = CGRectMake(100/WIDTH_6_SCALE, 35/WIDTH_6_SCALE + 24, SCREEN_WIDTH - 20/WIDTH_6_SCALE, 19/WIDTH_6_SCALE);
        }else{
            _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 264/WIDTH_6_SCALE);
            backBtn.frame = CGRectMake(18/WIDTH_6_SCALE, 14/WIDTH_6_SCALE + 20, 24/WIDTH_6_SCALE, 30/WIDTH_6_SCALE);
            pageTitleLab.frame = CGRectMake(100/WIDTH_6_SCALE, 35/WIDTH_6_SCALE, SCREEN_WIDTH - 20/WIDTH_6_SCALE, 19/WIDTH_6_SCALE);
        }
        pageTitleLab.centerX = SCREEN_WIDTH/2;
        [self label:pageTitleLab withFont:DEF_FontSize_14 withText:@"我的凭证" withTextColor:[UIColor whiteColor] withTextAlignment:NSTextAlignmentCenter withView:_headerView withBGColor:[UIColor clearColor]];
        [backBtn setImage:[UIImage imageNamed:@"up_back_icon"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:backBtn];
        
        //白色背景图案
        UIImageView * whiteImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, pageTitleLab.bottom + 21/WIDTH_6_SCALE, SCREEN_WIDTH - 30/WIDTH_6_SCALE, 175/WIDTH_6_SCALE)];
        whiteImgView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:whiteImgView];
        //账户余额标题
        UILabel * accountTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 44/WIDTH_6_SCALE, whiteImgView.width, 14/WIDTH_6_SCALE)];
        [self label:accountTitleLab withFont:DEF_FontSize_12 withText:@"账户余额(元)" withTextColor:getColor(@"9b9b9b") withTextAlignment:NSTextAlignmentCenter withView:whiteImgView withBGColor:[UIColor clearColor]];
        //账户余额具体数额
        self.accountContentLab = [[UILabel alloc] initWithFrame:CGRectMake(0, accountTitleLab.bottom + 19/WIDTH_6_SCALE, whiteImgView.width, 19/WIDTH_6_SCALE)];
        [self label:self.accountContentLab withFont:DEF_FontSize_16 withText:self.accountContentStr withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:whiteImgView withBGColor:[UIColor clearColor]];
        //立即充值
        int rechargeBtnW = 121/WIDTH_6_SCALE;
        int rechargeBtnH = 35/WIDTH_6_SCALE;
        UIButton * rechargeBtn = [[UIButton alloc] initWithFrame:CGRectMake(rechargeBtnH/2, self.accountContentLab.bottom + 22/WIDTH_6_SCALE, SCREEN_WIDTH - rechargeBtnW * 2, rechargeBtnH)];
        [self button:rechargeBtn withBGColor:getColor(greenBGColor) withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:rechargeBtnH/2 withMasksToBounds:YES withViwe:whiteImgView];
        [rechargeBtn setTitle:@"立即充值" forState:UIControlStateNormal];
        [rechargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rechargeBtn setBackgroundImage:[UIImage imageNamed:@"maself_bg"] forState:UIControlStateNormal];
        rechargeBtn.titleLabel.font = DEF_FontSize_14;
        rechargeBtn.centerX = self.accountContentLab.centerX;
        //箭头
        int arrowImgW = 8/WIDTH_6_SCALE;
        int arrowImgH = 12/WIDTH_6_SCALE;
        int arrowImgRightX = 16/WIDTH_6_SCALE;
        int arrowImgTopY = 60/WIDTH_6_SCALE;
        UIImageView * arrowImg = [[UIImageView alloc] initWithFrame:CGRectMake(whiteImgView.width - arrowImgW - arrowImgRightX, arrowImgTopY, arrowImgW, arrowImgH)];
        arrowImg.image = [UIImage imageNamed:@"next_icon"];
        [whiteImgView addSubview:arrowImg];
    }return _headerView;
}
- (NSMutableArray *)cellArr{
    if (!_cellArr) {
        _cellArr = @[].mutableCopy;
    }return _cellArr;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCertificateTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyCertificateTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellArr[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CertificateDetailViewController * vc = [[CertificateDetailViewController alloc] init];
    vc.helpid = [self.cellArr[indexPath.row] objectForKey:@"helpid"];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - action
- (void)backBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
